package com.xuanyuanxinyu.core.coroutines

import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout

/**
 * @FileName:CoroutinesCore
 * @Description: 协程功能核心类
 * @author ken.luo
 * @date 2023/4/4
 * @version V1.0
 * @email 424533553@qq.com
 */
@OptIn(DelicateCoroutinesApi::class)
class CoroutinesCore {
    /**
     * 生命周期Coroutines协程
     */
    private suspend fun lifeCoroutines() = with(Dispatchers.IO) {
        delay(2000)
    }
    
    suspend fun test() {
        GlobalScope.launch(Dispatchers.Main) {
            withTimeout(1000L) {
                lifeCoroutines()
                val a = 4 / 8
            }
        }
        val job = Job()
        val job2 = GlobalScope.async(Dispatchers.Main) {
            withTimeout(1000L) {
                lifeCoroutines()
                val a = 4 / 8
                return@withTimeout
            }
        }
        job2.await()
    }
    
}